<?php

/**
 * coligacao actions.
 *
 * @package    siscampanha
 * @subpackage coligacao
 * @author     Alexandre Haguiar
 */
class coligacaoActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->Coligacaos = ColigacaoPeer::doSelect(new Criteria());
        
        $this->Campanha = CampanhaPeer::retrieveByPK($request->getParameter('campanha_id'));    
    }

    public function executeShow(sfWebRequest $request) {
        $this->Coligacao = ColigacaoPeer::retrieveByPk($request->getParameter('id'));

        $this->forward404Unless($this->Coligacao);
    }

    public function executeNew(sfWebRequest $request) {
        $Coligacao = new Coligacao();
        $Coligacao->setCampanhaId($request->getParameter('campanha_id'));

        $this->form = new ColigacaoForm($Coligacao);
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new ColigacaoForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($Coligacao = ColigacaoPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Coligacao does not exist (%s).', $request->getParameter('id')));
        $this->form = new ColigacaoForm($Coligacao);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($Coligacao = ColigacaoPeer::retrieveByPk($request->getParameter('id'), $request->getParameter('campanha_id')), sprintf('Object Coligacao does not exist (%s).', $request->getParameter('id'), $request->getParameter('campanha_id')));
        $this->form = new ColigacaoForm($Coligacao);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($Coligacao = ColigacaoPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Coligacao does not exist (%s).', $request->getParameter('id')));
        $Coligacao->delete();

        $this->redirect('coligacao/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $Coligacao = $form->save();

            $this->redirect('coligacao/edit?id=' . $Coligacao->getId() . '&campanha_id=' . $Coligacao->getCampanhaId());
        }
    }

}
